/**
 *****************************************************************************
 * @file
 * @brief
 * @author luffy
 * @version V0.0.1
 * @date 2022/12/13
 ****************************************************************************
 */
  /* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "func_pt100.h"

#define TBLSIZE   86      //PT100分度表项目数 

typedef struct
{
    float temperature;
    float resistance;
}PT100TBL,*PPT100TBL;

/*-----------------------------------------------------------------------------
                            数据结构定义
-----------------------------------------------------------------------------*/
//IEC751标准PT100分度表，范围-200℃~650℃，间隔10℃
static const PT100TBL pt100Table[TBLSIZE] = 
{
    {-200.0F,18.52},
    {-190.0F,22.83},  
    {-180.0F,27.10}, 
    {-170.0F,31.34},
    {-160.0F,35.54},  
    {-150.0F,39.72},    
    {-140.0F,43.88},
    {-130.0F,48.00},  
    {-120.0F,52.11}, 
    {-110.0F,56.19},
    {-100.0F,60.26},  
    {-90.0F, 64.30},  
    {-80.0F, 68.33},  
    {-70.0F, 72.33},  
    {-60.0F, 76.33},  
    {-50.0F, 80.31},  
    {-40.0F, 84.27},  
    {-30.0F, 88.22},  
    {-20.0F, 92.16},  
    {-10.0F, 96.09},  
    {0.0F,   100.00},  
    {10.0F,  103.90},  
    {20.0F,  107.79},  
    {30.0F,  111.67},  
    {40.0F,  115.54},  
    {50.0F,  119.40},  
    {60.0F,  123.24},  
    {70.0F,  127.08},  
    {80.0F,  130.90},  
    {90.0F,  134.71},  
    {100.0F, 138.51},  
    {110.0F, 142.29},  
    {120.0F, 146.07},  
    {130.0F, 149.83},  
    {140.0F, 153.58},    
    {150.0F, 157.33},
    {160.0F, 161.05},
    {170.0F, 164.77},
    {180.0F, 168.48},
    {190.0F, 172.17},
    {200.0F, 175.86},
    {210.0F, 179.53},
    {220.0F, 183.19},
    {230.0F, 186.84},
    {240.0F, 190.47},
    {250.0F, 194.10},
    {260.0F, 197.71},
    {270.0F, 201.31},
    {280.0F, 204.90},
    {290.0F, 208.48},
    {300.0F, 212.05},
    {310.0F, 215.61},
    {320.0F, 219.15},
    {330.0F, 222.68},
    {340.0F, 226.21},
    {350.0F, 229.72},
    {360.0F, 233.21},
    {370.0F, 236.70},
    {380.0F, 240.18},
    {390.0F, 243.64},
    {400.0F, 247.09},
    {410.0F, 250.53},
    {420.0F, 253.96},
    {430.0F, 257.38},
    {440.0F, 260.78},
    {450.0F, 264.18},
    {460.0F, 267.56},
    {470.0F, 270.93},
    {480.0F, 274.29},
    {490.0F, 277.64},    
    {500.0F, 280.98},
    {510.0F, 284.30},
    {520.0F, 287.62},
    {530.0F, 290.92},
    {540.0F, 294.21},
    {550.0F, 297.49},
    {560.0F, 300.75},
    {570.0F, 304.01},
    {580.0F, 307.25},
    {590.0F, 310.49}, 
    {600.0F, 313.71},    
    {610.0F, 316.92},    
    {620.0F, 320.12},
    {630.0F, 323.30},
    {640.0F, 326.48},
    {650.0F, 329.64}    
};

/*-----------------------------------------------------------------------------
                            内部函数声明
-----------------------------------------------------------------------------*/
//NA
/*---------------------------------------------------------------------------------------
 函数原型: bool PT100_R2T(float resistance,float *pTmperature)
 功    能: 查表法阻值温度换算
 输入参数: resistance, PT100电阻值,单位欧姆
 输出参数: pTemperature, 温度值，单位℃
 返 回 值: true，成功; false, 失败
---------------------------------------------------------------------------------------*/
bool PT100_R2T(float resistance,float *pTemperature)
{
    int32_t i;
    int32_t low;
    int32_t high;
    float   deltaT;
    float   deltaR;
    
    if(resistance < pt100Table[0].resistance || resistance > pt100Table[TBLSIZE-1].resistance)
    {
        return false;
    }
    
    for(i=0; i < TBLSIZE-1; i++)
    {
        low = i;
        high = low+1;
        if(pt100Table[high].resistance >= resistance)
        {
            break;  //find in table
        }
    }
    
    //用线性插值法计算电阻值对应的温度值
    deltaR = pt100Table[high].resistance - pt100Table[low].resistance;
    deltaT = pt100Table[high].temperature - pt100Table[low].temperature;
   
    *pTemperature = pt100Table[low].temperature + (resistance - pt100Table[low].resistance)*(deltaT/deltaR);
    
    return true;
}

/*---------------------------------------------------------------------------------------
 函数原型: bool PT100_T2R(float temperature,float *pResistance)
 功    能: 查表法PT100阻值温度换算
 输入参数: temperature,  温度值，单位℃
 输出参数: pResistance, PT100电阻值,单位欧姆
 返 回 值: true，成功; false, 失败
---------------------------------------------------------------------------------------*/
bool PT100_T2R(float temperature, float *pResistance)
{
    int32_t i;
    int32_t low;
    int32_t high;
    float   deltaT;
    float   deltaR;
    
    if(temperature < pt100Table[0].temperature || temperature > pt100Table[TBLSIZE-1].temperature)
    {
        return false;
    }
    
    for(i=0; i < TBLSIZE-1; i++)
    {
        low = i;
        high = low+1;
        if(pt100Table[high].temperature <= temperature)
        {
            break;  //find in table
        }
    }
    
    //用线性插值法计算电阻值对应的温度值
    deltaR = pt100Table[high].resistance - pt100Table[low].resistance;
    deltaT = pt100Table[high].temperature - pt100Table[low].temperature;
   
    *pResistance = pt100Table[low].resistance + (temperature - pt100Table[low].temperature)*(deltaR/deltaT);
    
    return true;
}

